using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using MyApi.Entity;
using MyApi.ParamModels;
using Newtonsoft.Json;
using MyApi.Repository;
namespace MyApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UserController : ControllerBase
    {
        //定义一个私有字段,实例化一个IRepository
        private IRepository<User> userRepository;
        //构造函数
        public UserController(IRepository<User> usersRepository)
        {
            userRepository = usersRepository;
        }
        //查询用户列表
        [HttpGet]
        public dynamic GetDynamic()
        {
            //查询所有用户
            var users = userRepository.Table.ToList();
            //返回
            return new
            {
                Code = 200,
                Data = users,
                Msg = "获取用户列表成功！"
            };
        }
        //查询指定用户
        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            //获取指定Id用户
            var user = userRepository.GetById(id);
            //返回结果
            return user;
        }

        //添加
        [HttpPost]
        public dynamic Post(NewUser model)
        {
            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

            //获取用户账号密码
            var user = new User
            {
                UserName = model.UserName,
                PassWord = model.PassWord
            };
            //把信息插入数据库中
            userRepository.Insert(user);

            //成功信息
            var msg = new
            {
                Code = 200,
                Data = user,
                Msg = "数据插入成功！"
            };
            return JsonConvert.SerializeObject(msg, settings);
        }

        //修改
        [HttpPut("{id}")]
        public dynamic Update(int id, NewUser model)
        {
            //找到要修改Id用户
            var user = userRepository.GetById(id);
            //用户不为空
            if (user != null)
            {
                user.UserName = model.UserName;
                user.PassWord = model.PassWord;
                user.UpdatedTime = DateTime.Now;
                userRepository.Update(user);
                //修改成功信息
                return new
                {
                    Code = 200,
                    Data = User,
                    Msg = String.Format("用户{0}信息修改成功！", id)
                };
            }
            else
            {
                //失败信息
                return new
                {
                    Code = 400,
                    Msg = string.Format("用户{0}不存在，修改失败", id)
                };
            }
        }
        //删除
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            //获取删除用户id
            var user=userRepository.GetById(id);
            //用户存在
            if(user!=null){
                //删除
                userRepository.Delete(id);
                //删除成功信息
                return new{
                    Code=200,
                    Msg=string.Format("用户{0}已删除！",id)
                };
            }
            else
            {
                return new{
                    Code=400,
                    Msg=string.Format("用户{0}删除失败，用户不存在！",id)
                };
            }
        }
    }
}